import Sequelize from 'sequelize'

const config = process.config.database

const sequelize = new Sequelize(config.database, config.username, config.password, config.options)

const User = sequelize.define('users', {
  id: { type: Sequelize.STRING, primaryKey: true }, // 微租车ID
  nickName: Sequelize.STRING, // 客户名称/公司名称
  phone: Sequelize.STRING, // 联系电话
  customerType: Sequelize.STRING, // 用户类型
  customerStatus: Sequelize.STRING, // 客户状态
  registerTime: Sequelize.STRING, // 注册时间
  submitAuditingTime: Sequelize.STRING, // 提交审核时间
  auditingTime: Sequelize.STRING, // 审核时间
  auditor: Sequelize.STRING, // 审核人
  userGroup: Sequelize.STRING, // 用户群组
  grabPosition: Sequelize.STRING, // 从哪里抓取的

  userLevel: Sequelize.STRING, // 会员等级
  userName: Sequelize.STRING, // 账号

  idCardNo: Sequelize.STRING, // 证件号
  idCardName: Sequelize.STRING, // 姓名
  idCardType: Sequelize.STRING, // 证件类型
  idCardSex: Sequelize.STRING, // 性别
  idCardBirthday: Sequelize.STRING, // 生日
  idCardNational: Sequelize.STRING, // 民族
  idCardHouseholdRegister: Sequelize.STRING, // 户籍
  idCardImage: Sequelize.STRING, // 证件图片

  drivingLicenceNo: Sequelize.STRING, // 驾驶证号
  drivingLicenceType: Sequelize.STRING, // 驾驶证类型
  drivingLicenceReceiveAddress: Sequelize.STRING, // 领证地区
  drivingLicenceFileNo: Sequelize.STRING, // 驾驶证档案编号
  drivingLicenceEndTime: Sequelize.STRING, // 驾驶证到期时间
  drivingLicenceImage: Sequelize.STRING, // 驾驶证图片

  failureReason: Sequelize.STRING, // 审核不通过理由

  again: {
    type: Sequelize.BOOLEAN,
    defaultValue: false
  }
})

User.sync()

export default {
  async insert(data) {
    const user = await User.findById(data.id)
    if (user) {
      await User.update(data, { where: {id: data.id} })
    } else {
      await User.create(data)
    }
  },
  async update(data) {
    await User.update(data, { where: {id: data.id} })
  },
  async all() {
    const result = await User.findAll()
    return new Promise((resolve, reject) => {
      resolve(result)
    })
  },
  sequelize
}
